package com.fcy.teamwork.business.recommendstudent

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fcy.teamwork.App
import com.fcy.teamwork.base.BaseActivity
import com.fcy.teamwork.base.fromJson
import com.fcy.teamwork.databinding.ActivityRecommendStudentBinding
import com.fcy.teamwork.pojo.Job
import kotlinx.coroutines.launch

class RecommendStudentActivity : BaseActivity<ActivityRecommendStudentBinding>() {
    private lateinit var job: Job
    private val vm: RecommendStudentVM by viewModels()
    private val adapter: RecommendAdapter = RecommendAdapter()
    override fun initBinding() = ActivityRecommendStudentBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        parseArgs()
        binding.actionBar.apply {
            tvTitle.text = "系统推荐学生"
            ibFinish.visibility = View.VISIBLE
            ibFinish.setOnClickListener {
                lifecycleScope.launch {
                    vm.sendInvitation(adapter.getAllSelected(), job)
                }
                finish()
            }
        }
        binding.rv.apply {
            layoutManager = LinearLayoutManager(this@RecommendStudentActivity)
            this.adapter = this@RecommendStudentActivity.adapter
        }
        lifecycleScope.launchWhenCreated {
            adapter.submitData(vm.loadRecommends(job).filter { person -> person.personId != App.person.personId })
        }
    }

    private fun parseArgs() {
        job = (intent.getStringExtra("job")
            ?: throw java.lang.IllegalStateException("")).fromJson()

    }
}